﻿namespace Nello.Web.Controls
{
    using Nello;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web.UI.WebControls;

    public class NGridView : GridView
    {
        private int chars = 0x10;
        private bool fullrowselect = true;
        private bool hidefirstcolumn = true;
        private bool limitcellchars = true;

        public DataControlField Column(string headerText)
        {
            foreach (DataControlField col in this.Columns)
            {
                if (col.HeaderText == headerText)
                {
                    return col;
                }
            }
            return null;
        }

        protected override void CreateChildControls()
        {
            try
            {
                this.Page.Form.Controls.Add(new LinkButton());
            }
            catch
            {
            }
            base.CreateChildControls();
        }

        public void DoDataBind(object dataSource)
        {
            this.DataSource = dataSource;
            this.DataBind();
        }

        [Obsolete]
        public void DoDefaultBind()
        {
            this.DataSource = new DataTable();
            this.DataBind();
        }

        public double GetTotals(int colIndex)
        {
            double sum = 0.0;
            double tmp = 0.0;
            foreach (GridViewRow row in this.Rows)
            {
                double.TryParse(row.Cells[colIndex].Text, out tmp);
                sum += tmp;
            }
            return sum;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            base.SelectedRowStyle.BackColor = Color.RoyalBlue;
            base.SelectedRowStyle.ForeColor = Color.White;
            base.HeaderStyle.BackColor = Color.SteelBlue;
            base.HeaderStyle.ForeColor = Color.White;
        }

        protected override void OnRowDataBound(GridViewRowEventArgs e)
        {
            base.OnRowDataBound(e);
            if ((e.Row.Cells.Count > 0) && this.hidefirstcolumn)
            {
                e.Row.Cells[0].Attributes.Add("style", "display:none");
            }
            if ((e.Row.RowType != DataControlRowType.Header) && (e.Row.RowType != DataControlRowType.Footer))
            {
                if (this.limitcellchars)
                {
                    foreach (TableCell cell in e.Row.Cells)
                    {
                        cell.ToolTip = cell.Text;
                        if (!((cell.Text.Length <= this.chars) || Util.IsGuid(cell.Text)))
                        {
                            cell.Text = cell.Text.Substring(0, this.chars) + "...";
                        }
                    }
                }
                if ((e.Row.RowType == DataControlRowType.DataRow) && this.fullrowselect)
                {
                    e.Row.Attributes.Add("onclick", this.Page.ClientScript.GetPostBackClientHyperlink(this, "SelectedIndex=" + e.Row.RowIndex));
                    e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#98e000'");
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
                    e.Row.Style.Add("cursor", "hand");
                }
            }
        }

        protected override void RaisePostBackEvent(string eventArgument)
        {
            if (eventArgument.StartsWith("SelectedIndex"))
            {
                string index = eventArgument.TrimStart("SelectedIndex=".ToCharArray());
                this.SelectedIndex = int.Parse(index);
                this.OnSelectedIndexChanged(null);
            }
            else
            {
                base.RaisePostBackEvent(eventArgument);
            }
        }

        public void SetColumnCssClass(int colIndex, string css)
        {
            this.Columns[colIndex].HeaderStyle.CssClass = css;
            this.Columns[colIndex].ItemStyle.CssClass = css;
            this.Columns[colIndex].FooterStyle.CssClass = css;
        }

        public bool FullRowSelect
        {
            get
            {
                return this.fullrowselect;
            }
            set
            {
                this.fullrowselect = value;
            }
        }

        public bool HideFirstColumn
        {
            get
            {
                return this.hidefirstcolumn;
            }
            set
            {
                this.hidefirstcolumn = value;
            }
        }

        public bool LimitCellChars
        {
            get
            {
                return this.limitcellchars;
            }
            set
            {
                this.limitcellchars = value;
            }
        }

        [Description("一个单元格内最多显示的字符数")]
        public int MaxCellChars
        {
            get
            {
                return this.chars;
            }
            set
            {
                this.chars = value;
            }
        }
    }
}

